﻿#include "event_reporter_manager.h"
#include "event_reporter.h"
#include <iostream>

    INSTANCE_SINGLETON(EventReporterManager)

    bool EventReporterManager::AddEventCallInfo(const EventCallReporter *reporter)
    {
        if (!reporter)
        {
            return false;
        }
        auto it_obj = all_event_call_info_.find(reporter->obj);
        if (it_obj == all_event_call_info_.end())
        {
            auto it = all_event_call_info_.emplace(reporter->obj, reporter->event_call_info);
            return it.second;
        }
        else
        {// 增量叠加
            for (auto &it: reporter->event_call_info)
            {
                it_obj->second[it.first] += it.second;
            }
        }
        return true;
    }

    void EventReporterManager::Dump(const EventCallReporter::ObjPtrType obj) const
    {
        auto it_obj = all_event_call_info_.find(obj);
        if (it_obj == all_event_call_info_.end())
        {
            return;
        }
        for (auto &it_event: it_obj->second)
        {
            std::cout << "filename,line_no,event_type,call_times:" 
                << it_event.first.first << "," 
                << it_event.first.second << ","
                << it_event.second.event_type << ","
                << it_event.second.call_times << std::endl;
        }
    }

    bool EventReporterManager::Clear(const EventCallReporter::ObjPtrType obj)
    {
        return all_event_call_info_.erase(obj) != 0u;
    }

    void EventReporterManager::Clear()
    {
        all_event_call_info_.clear();
    }